/**
 * 
 */
package com.epart.widget.module.component;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.epart.widget.module.board.BoardService;
import com.epart.widget.module.login.LoginService;
import com.epart.widget.module.wcmswidget.WcmsWidgetService;

/**
 * 기본 위젯
 * 
 * @author cwsong
 * @version 2011. 3. 10.
 */
public class WcmsWidget extends JFrame {

	private static int DEFAULT_WIDTH = 300;
	private static int DEFAULT_HEIGHT = 200;
	private static final int DEFAULT_LAF = 1;

	private WcmsWidgetService wcmsWidgetService;
	private LoginService loginService;
	private BoardService boardService;

	private WcmsWidgetMenuBar wcmsWidgetMenuBar;
	private WcmsWidgetLogin wcmsWidgetLogin;

	/**
	 * @param loginService
	 *            the loginService to set
	 */
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	/**
	 * @param boardService
	 *            the boardService to set
	 */
	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}

	/**
	 * @param wcmsWidgetLogin
	 *            the wcmsWidgetLogin to set
	 */
	public void setWcmsWidgetLogin(WcmsWidgetLogin wcmsWidgetLogin) {
		this.wcmsWidgetLogin = wcmsWidgetLogin;
	}

	/**
	 * @param wcmsWidgetMenuBar
	 *            the wcmsWidgetMenuBar to set
	 */
	public void setWcmsWidgetMenuBar(WcmsWidgetMenuBar wcmsWidgetMenuBar) {
		this.wcmsWidgetMenuBar = wcmsWidgetMenuBar;
	}

	/**
	 * @param wcmsWidgetService
	 *            the wcmsWidgetService to set
	 */
	public void setWcmsWidgetService(WcmsWidgetService wcmsWidgetService) {
		this.wcmsWidgetService = wcmsWidgetService;
	}
	
	/**
	 * 생성자
	 */
	public WcmsWidget() {
		getContentPane().setBackground(Color.WHITE);
		setLookAndFeel(DEFAULT_LAF);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		try {
			Image icon = ImageIO.read(ClassLoader.getSystemResourceAsStream("images/icon.png"));
			setIconImage(icon);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 룩앤필(스킨)을 변경한다.
	 * 
	 * @param no
	 */
	public void setLookAndFeel(int no) {
		try {
			switch (no) {
			case DEFAULT_LAF:
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				break;
			}
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (UnsupportedLookAndFeelException e) {
		}
	}
	
	/**
	 * 위젯을 구동한다. 
	 */
	public void start() {

		wcmsWidgetLogin.setWcmsWidget(this);
		wcmsWidgetLogin.setLoginService(loginService);
		add(wcmsWidgetLogin);

		wcmsWidgetMenuBar.setWcmsWidget(this);
		wcmsWidgetMenuBar.setBoardService(boardService);
		wcmsWidgetMenuBar.createMenuBar();
		wcmsWidgetMenuBar.setVisible(false);
		setJMenuBar(wcmsWidgetMenuBar);

		setTitle(wcmsWidgetService.getSiteName());
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		int locationX = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - DEFAULT_WIDTH / 2);
		int locationY = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - DEFAULT_HEIGHT / 2);
		setLocation(locationX, locationY);
		setVisible(true);
	}
	
	/**
	 * 로그인성공후 로그인 컴포넌트를 삭제하고 메뉴를 보여준다.
	 */
	public void loadWcmsWidgetMain() {
		wcmsWidgetMenuBar.setVisible(true);
		wcmsWidgetLogin.getParent().remove(wcmsWidgetLogin);
	}

}
